const { createApp } = Vue;

createApp({
    data() {
        return {
            userType: 'client',
            showPassword: false,
            loading: false,
            registerForm: {
                phone: '',
                password: '',
                confirmPassword: '',
                name: '',
                gender: 'male',
                agreement: false
            }
        };
    },
    methods: {
        setUserType(type) {
            console.log('切换用户类型:', type);
            this.userType = type;
        },
        togglePasswordVisibility() {
            this.showPassword = !this.showPassword;
        },
        validateForm() {
            if (!this.registerForm.phone) {
                alert('请输入手机号码');
                return false;
            }
            const phoneRegex = /^1[3-9]\d{9}$/;
            if (!phoneRegex.test(this.registerForm.phone)) {
                alert('请输入正确的手机号码');
                return false;
            }
            if (!this.registerForm.password) {
                alert('请设置密码');
                return false;
            }
            if (this.registerForm.password.length < 6 || this.registerForm.password.length > 20) {
                alert('密码长度应为6-20位');
                return false;
            }
            if (this.registerForm.password !== this.registerForm.confirmPassword) {
                alert('两次输入的密码不一致');
                return false;
            }
            if (!this.registerForm.name) {
                alert('请输入真实姓名');
                return false;
            }
            if (!this.registerForm.agreement) {
                alert('请阅读并同意用户协议和隐私政策');
                return false;
            }
            return true;
        },
        async handleRegister() {
            console.log('点击注册按钮');
            if (!this.validateForm()) {
                return;
            }

            const requestData = {
                phone: this.registerForm.phone,
                password: this.registerForm.password,
                name: this.registerForm.name,
                gender: this.registerForm.gender === 'male' ? '男' : '女',
                userType: this.userType
            };

            console.log('请求数据:', requestData);

            this.loading = true;
            try {
                const response = await axios({
                    method: 'POST',
                    url: '/api/auth/register',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: requestData,
                    timeout: 10000
                });

                console.log('注册响应:', response.data);

                if (response.data.code === 200) {
                    alert('注册成功！');
                    window.location.href = 'login.html';
                } else {
                    alert(response.data.message || '注册失败，请稍后重试');
                }
            } catch (error) {
                console.error('注册失败:', error);
                if (error.response) {
                    console.error('错误状态码:', error.response.status);
                    console.error('错误数据:', error.response.data);
                    alert(error.response.data?.message || '注册失败，请稍后重试');
                } else if (error.request) {
                    console.error('没有收到响应');
                    alert('服务器无响应，请确保后端服务已启动');
                } else {
                    console.error('请求配置错误:', error.message);
                    alert('请求错误，请稍后重试');
                }
            } finally {
                this.loading = false;
            }
        }
    }
}).mount('#app'); 